#include "CancelSafeQueue.h"

extern IO_CSQ g_CancelSafeQueue;
extern LIST_ENTRY g_TransportIrpQueue;
extern KSPIN_LOCK g_QueueLock;

VOID QInsertIrp (
				 __in PIO_CSQ Csq,
				 __in PIRP pIrp
				 )
{
	InsertTailList(&g_TransportIrpQueue, &pIrp->Tail.Overlay.ListEntry);
}

VOID QRemoveIrp(
				__in  PIO_CSQ Csq,
				__in  PIRP    pIrp
				)
{
	UNREFERENCED_PARAMETER(Csq);

	RemoveEntryList(&pIrp->Tail.Overlay.ListEntry);
}

PIRP QPeekNextIrp(
				  __in  PIO_CSQ Csq,
				  __in  PIRP pIrp,
				  __in  PVOID   PeekContext
				  )
{
	PIRP				nextIrp = NULL;
	PLIST_ENTRY			nextEntry;
	PLIST_ENTRY			listHead;
	PIO_STACK_LOCATION	irpStack;

	listHead = &g_TransportIrpQueue;

	if (pIrp == NULL)
	{
		nextEntry = listHead->Flink;
	} 
	else
	{
		nextEntry = pIrp->Tail.Overlay.ListEntry.Flink;
	}

	if(nextEntry != listHead)
	{
		nextIrp = CONTAINING_RECORD(nextEntry, IRP, Tail.Overlay.ListEntry);
	}

	return nextIrp;
}

VOID QAcquireLock(
				  __in	PIO_CSQ Csq,
				  __out PKIRQL  Irql
				  )
{
	KeAcquireSpinLock(&g_QueueLock, Irql);
}

VOID QReleaseLock(
				  __in PIO_CSQ Csq,
				  __in KIRQL   Irql
				  )
{
	KeReleaseSpinLock(&g_QueueLock, Irql);
}

VOID QCompleteCanceledIrp(
						  __in  PIO_CSQ	pCsq,
						  __in  PIRP	pIrp
						  )
{

	UNREFERENCED_PARAMETER(pCsq);

	pIrp->IoStatus.Status = STATUS_CANCELLED;
	pIrp->IoStatus.Information = 0;
	KdPrint(("Cancelled pIrp\n"));
	IoCompleteRequest(pIrp, IO_NO_INCREMENT);
}